﻿using System.Collections.Concurrent;
using IceZero.Business.Business;
using IceZero.Business.Models;
using IceZero.Common.Services;
using Microsoft.AspNetCore.Mvc;

namespace IceZero.Web.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class OnlineContainerController : ControllerBase
    {
        private readonly OnlineContainerBusiness _onlineContainerBusiness;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="onlineContainerBusiness"></param>
        public OnlineContainerController(OnlineContainerBusiness onlineContainerBusiness)
        {
            _onlineContainerBusiness = onlineContainerBusiness;
        }
        /// <summary>
        /// 添加在线对象
        /// </summary>
        /// <param name="args"></param>
        [HttpPost]
        public string Add(OnlineContainerAddArgs args)
        {
            return _onlineContainerBusiness.Add(args.AreaName, args.Key, args.Data, args.ExpireSeconds);
        }
        /// <summary>
        /// 主动删除区域内所有对象
        /// </summary>
        /// <param name="areaName">区域名</param>
        /// <returns></returns>
        [HttpGet("{areaName}")]
        public bool Delete(string areaName)
        {
            return _onlineContainerBusiness.Delete(areaName);
        }

        /// <summary>
        /// 删除域内某个key的对象
        /// </summary>
        /// <param name="areaName">区域名</param>
        /// <param name="key">键</param>
        /// <returns></returns>
        [HttpGet("{areaName}/{key}")]
        public bool DeleteByAreaKey(string areaName, string key)
        {
            return _onlineContainerBusiness.DeleteByAreaKey(areaName, key);
        }

        /// <summary>
        /// 获取域里面的某个键的对象
        /// </summary>
        /// <param name="areaName">区域名</param>
        /// <param name="key">键</param>
        [HttpGet("{areaName}/{key}")]
        public OnlineObject Get(string areaName, string key)
        {
            return _onlineContainerBusiness.Get(areaName, key);
        }

        /// <summary>
        /// 获取域里面的所有的对象
        /// </summary>
        /// <param name="areaName">区域名</param>
        /// <returns></returns>
        [HttpGet("{areaName}")]
        public ConcurrentDictionary<string, OnlineObject> GetAll(string areaName)
        {
            return _onlineContainerBusiness.GetAll(areaName);
        }
        /// <summary>
        /// 获取域里面的所有对象总数
        /// </summary>
        /// <param name="areaName">区域名</param>
        /// <returns></returns>
        [HttpGet("{areaName}")]
        public int GetCount(string areaName)
        {
            return _onlineContainerBusiness.GetCount(areaName);
        }

        /// <summary>
        /// 获取域里面的所有对象总数
        /// </summary>
        /// <param name="areaName">区域名</param>
        /// <param name="dataKey">查询在线对象（OnlineObject）中的Data的key值</param>
        /// <param name="dataValue">查询在线对象（OnlineObject）中的Data的Value值</param>
        /// <returns></returns>
        [HttpGet("{areaName}/{dataKey}/{dataValue}")]
        public int GetCountWhere(string areaName, string dataKey, string dataValue)
        {
            return _onlineContainerBusiness.GetCountWhere(areaName, dataKey, dataValue);
        }
    }
}
